package myPetStore.web.servlet;

import myPetStore.domain.Account;
import myPetStore.service.EmailService;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class VerificationServlet extends HttpServlet {

    private String verificationCode;
    private String userEmail;
    private String repeatPassword;
    private String from;//请求来自哪个页面

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.from=req.getParameter("getCode");
        userEmail= req.getParameter("email");
        resp.setContentType("text/plain");
        PrintWriter out= resp.getWriter();
        if(from!=null&&!from.equals("")){
            if(from.equals("getVerification")){
                //发送验证码，返回页面为登录界面
                if(!isValidate()){
                    //用输出流返回
                    out.println("Please enter a reasonable email");
                }else{
                    createVerification();
                    HttpSession session=req.getSession();
                    session.setAttribute("verification",this.verificationCode);
                    //TODO 输出
                    System.out.println(this.verificationCode);
                    req.setAttribute("inputEmail",userEmail);
                    req.setAttribute("token","");
                    try {
                        sendCodeToEmail();
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                    out.println(this.verificationCode);
                }
            }else if(from.equals("Get a verification code")){
                //获取传送的邮箱,检查邮箱合法性
                if(!isValidate()){
                    req.setAttribute("registerMsg","Please enter a reasonable email");
                    out.println("Please enter a reasonable email");
                }else{
                    //创建verification code 并且放入session中
                    createVerification();
                    req.setAttribute("verification",this.verificationCode);
                    //把填入的信息放入对象中
                    //发送给用户，
                    try {
                        sendCodeToEmail();
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                    out.println(this.verificationCode);
                }
            }
        }





    }
    private void sendCodeToEmail() throws MessagingException {
        EmailService emailService=new EmailService(verificationCode,userEmail);
        //            emailService.sendEmail(verificationCode,userEmail);
        emailService.start();
    }


    private boolean isValidate(){
        return userEmail != null && !userEmail.equals("");
//        else{
//            return userEmail.contains("@");
//        }
    }
    private void createVerification(){
        int tempcode=(int)(Math.random()*10000)+1000;
        verificationCode=tempcode+"";
    }
    private void initAccount(Account account,HttpServletRequest req){
        account.setUsername(req.getParameter("username"));
        account.setPassword(req.getParameter("password"));
        account.setFirstName(req.getParameter("firstname"));
        account.setLastName(req.getParameter("lastname"));
        account.setEmail(req.getParameter("email"));
        account.setCountry(req.getParameter("country"));
        account.setState(req.getParameter("state"));
        account.setCity(req.getParameter("city"));
        account.setAddress1(req.getParameter("address1"));
        account.setAddress2(req.getParameter("address2"));
        account.setZip(req.getParameter("zip"));
        account.setPhone(req.getParameter("phone"));
        account.setFavouriteCategoryId(req.getParameter("favcategory"));
        repeatPassword=req.getParameter("repeatPassword");
    }
}
